using System;
using System.IO;
using System.Xml;

namespace SanFS.DiscLibrary
{
	[Serializable]
	public class DiscFile : IDiscFile
	{
		private	Guid	mUuid;
		private string	mPath;
		private ulong	mTotalSize;
		private int		mTotalChunks;
		private int		mChunkNumber;
		private ulong	mChunkSize;
		private ulong	mChunkOffset;
		
		public DiscFile(XmlNode CfgNode)
		{
			mUuid = new Guid(CfgNode.Attributes.GetNamedItem("Uuid").Value);
			mTotalSize = Convert.ToUInt64(CfgNode.Attributes.GetNamedItem("TotalSize").Value);
			mTotalChunks = Convert.ToInt32(CfgNode.Attributes.GetNamedItem("TotalChunks").Value);
			mChunkNumber = Convert.ToInt32(CfgNode.Attributes.GetNamedItem("ChunkNumber").Value);
			mChunkSize = Convert.ToUInt64(CfgNode.Attributes.GetNamedItem("ChunkSize").Value);
			mChunkOffset = Convert.ToUInt64(CfgNode.Attributes.GetNamedItem("ChunkOffset").Value);
			mPath = CfgNode.Attributes.GetNamedItem("Path").Value;	
		}
		
		public DiscFile(string DiscRelativeDirPath, FileInfo FInfo)
		{
			mUuid = Guid.NewGuid();
			mTotalChunks = 1; // XXX:
			mChunkNumber = 0;
			mTotalSize = (ulong) FInfo.Length;
			mChunkSize = mTotalSize;
			mChunkOffset = 0;
			if (System.IO.Path.GetFileName(DiscRelativeDirPath) != FInfo.Name)
				throw new DiscManagerException("RelativeDirPath does not end with filename");
			mPath = DiscRelativeDirPath;
		}
		
		public DiscFile(Guid Uuid, UInt64 TotalSize, Int32 TotalChunks, String Path, Int32 ChunkNumber, UInt64 ChunkSize, UInt64 ChunkOffset) {
			this.mUuid = Uuid;
			this.mTotalChunks = TotalChunks;
			this.mTotalSize = TotalSize;
			this.mPath = Path;
			this.mChunkNumber = ChunkNumber;
			this.mChunkSize = ChunkSize;
			this.mChunkOffset = ChunkOffset;
		}
		
		public Guid Uuid
		{
			get { return mUuid; }
		}
		
		public string Path
		{
			get { return mPath; }
		}
		
		public int TotalChunks
		{
			get { return mTotalChunks; }
		}
		
		public int ChunkNumber
		{
			get { return mChunkNumber; }
		}
		
		public ulong ChunkSize
		{
			get { return mChunkSize; }
		}
		
		public ulong TotalSize
		{
			get { return mTotalSize; }
		}
		
		public ulong ChunkOffset
		{
			get { return mChunkOffset; }
		}
	}  // END CLASS
}  // END NAMESPACE
